package com.ilab.view.widget;

import com.ilab.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewDialog extends BaseDialog implements AdapterView.OnItemClickListener{
	private ListAdapter adapter = null;
	private AdapterView.OnItemClickListener itemClickListener = null;
	private Dialog dialog = null;
	
	public ListViewDialog(Context context, int title) {
		super(context, title);
	}
	public ListViewDialog(Context context,String title){
		super(context, title);
	}
	
	public ListViewDialog setDdapter(ListAdapter adapter){
		this.adapter = adapter;
		return this;
	}
	
	public ListViewDialog setOnItemClickListener(AdapterView.OnItemClickListener listener){
		this.itemClickListener = listener;
		return this;
	}
	
	public Dialog create(){
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		dialog = new Dialog(context, R.style.BaseDialog);
        View layout = inflater.inflate(R.layout.dialog_listview_base, null);
        dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        ((TextView) layout.findViewById(R.id.title)).setText(title);
        
        if (positiveTxt != null && positiveTxt.length()>0) {
            ((Button) layout.findViewById(R.id.positiveButton)).setText(positiveTxt);
            if (positiveButtonClickListener != null) {
                ((Button) layout.findViewById(R.id.positiveButton))
                        .setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                            }
                        });
            }
        } else {
            layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
        }
        
        if (negativeTxt != null && negativeTxt.length()>0) {
            ((Button) layout.findViewById(R.id.negativeButton)).setText(negativeTxt);
            if (negativeButtonClickListener != null) {
                ((Button) layout.findViewById(R.id.negativeButton))
                        .setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                           	 negativeButtonClickListener.onClick(dialog,DialogInterface.BUTTON_NEGATIVE);
                            }
                        });
            }
        } else {
            layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
        }
        
        ListView listView = (ListView)layout.findViewById(R.id.simpl_listview);
		if(this.adapter!=null){
			listView.setAdapter(adapter);
		}
		
		listView.setOnItemClickListener(this);
		dialog.setContentView(layout);
		return dialog;
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
		if(this.itemClickListener!=null){
			itemClickListener.onItemClick(arg0, arg1, arg2, arg3);
		}
		
		dialog.dismiss();
	}

}
